using System;
using System.Collections.Generic;
using System.Text;
using System.Resources;
using System.Collections.Specialized;
using System.Diagnostics;

namespace Medianamik.Globalization
{
    /// <summary>
    /// Implementation of IResourceReader required to retrieve a dictionary
    /// of resource values for implicit localization. 
    /// </summary>
    public class TranslationResourceReader : DisposableBaseType, IResourceReader, IEnumerable<KeyValuePair<string, object>>
    {
        private ListDictionary resourceDictionary;

        public TranslationResourceReader(ListDictionary resourceDictionary)
        {
            Debug.WriteLine("TranslationResourceReader()");
            this.resourceDictionary = resourceDictionary;
        }

        #region IResourceReader Members

        public void Close()
        {
            this.Dispose();
        }

        public System.Collections.IDictionaryEnumerator GetEnumerator()
        {
            Debug.WriteLine("DBResourceReader.GetEnumerator()");

            // NOTE: this is the only enumerator called by the runtime for 
            // implicit expressions

            if (Disposed)
            {
                throw new ObjectDisposedException("TranslationResourceReader object is already disposed.");
            }

            return this.resourceDictionary.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            if (Disposed)
            {
                throw new ObjectDisposedException("TranslationResourceReader object is already disposed.");
            }

            return this.resourceDictionary.GetEnumerator();
        }

        #endregion

        #region IEnumerable<KeyValuePair<string,object>> Members

        IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
        {
            if (Disposed)
            {
                throw new ObjectDisposedException("TranslationResourceReader object is already disposed.");
            }

            return this.resourceDictionary.GetEnumerator() as IEnumerator<KeyValuePair<string, object>>;
        }

        #endregion

        protected override void Cleanup()
        {
            try
            {
                this.resourceDictionary = null;
            }
            finally
            {
                base.Cleanup();
            }
        }
    }
}
